package com.yonyou.cyxdms.sysAuth.service.controller.dealerchannel;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyxdms.basedata.client.service.basedata.BaseCommonBrandService;
import com.yonyou.cyxdms.basedata.client.service.basedata.BaseCommonSeriesService;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import com.yonyou.cyxdms.sysAuth.service.dto.dealerchannel.DealerInfoDTO;
import com.yonyou.cyxdms.sysAuth.service.service.dealerchannel.AuthorizedInfoService;
import com.yonyou.cyxdms.sysAuth.service.service.dealerchannel.DealerChannelService;
import com.yonyou.cyxdms.sysAuth.service.service.dealerchannel.DealerInfoService;
import com.yonyou.cyxdms.sysAuth.service.service.org.OrgService;
import com.yonyou.cyxdms.sysAuth.service.vo.dealerchannel.DealerOrgVO;
import com.yonyou.cyxdms.sysAuth.service.vo.org.OrgByTypeVO;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by adu on 2019/10/10.
 */
@RestController
@RequestMapping("/dealerSaleInfo")
@Api(value = "经销商销售信息", tags = "经销商维护-销售相关")
public class DealerSaleInfoController {

    private final Logger logger = LoggerFactory.getLogger(DealerSaleInfoController.class);
    @Autowired
    public DealerInfoService dealerInfoService;
    @Autowired
    public AuthorizedInfoService authorizedInfoService;
    @Autowired
    private DealerChannelService dealerChannelService;
    @Autowired
    BaseCommonBrandService baseCommonBrandService;
    @Autowired
    BaseCommonSeriesService baseCommonSeriesService;
    @Autowired
    public OrgService orgService;

    /**
     * 查询-所属经销商公司（销售）
     */
    @GetMapping(value = "/getDistributor")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @ApiOperation(value = "查询-所属经销商公司（Name）")
    public List<Map<String, Object>> getDistributor() {
        return dealerChannelService.getDistributor();
    }

    /**
     * 查询-所属经销商公司（售后）
     */
    @GetMapping(value = "/getDistributors")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @ApiOperation(value = "查询-所属经销商公司（Name）")
    public List<Map<String, Object>> getDistributors() {
        return dealerChannelService.getDistributors();
    }


    /**
     * 查询-所属经销商公司all（销售）
     */
    @GetMapping(value = "/getDistributorALL")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @ApiOperation(value = "查询-所属经销商公司")
    public List<Map<String, Object>> getdistributorall() {
        return dealerChannelService.getdistributorall();
    }

    /**
     * 分页查询数据
     * adu
     *
     * @since 2019-10-9
     */
    @ResponseBody
    @GetMapping("/getSaleInfoMaintain")
    @ApiOperation(value = "销售信息维护（分页）查询")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string",
            paramType = "header")})
    public IPage<DealerInfoDTO> getSaleInfo(@RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                                            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                            String dealerCode, String dealerName, String provinceId, Integer cityId, String orgId) {
        Page<DealerInfoDTO> page = new Page<DealerInfoDTO>(currentPage, pageSize);
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        Map<String, Object> queryCondition = new HashMap<>();
        queryCondition.put("appId", loginInfoDto.getAppId());
        queryCondition.put("dealerCode", dealerCode);
        queryCondition.put("dealerName", dealerName);
        queryCondition.put("provinceId", provinceId);
        queryCondition.put("cityId", cityId);
        queryCondition.put("orgId", orgId);
        return dealerInfoService.selectPageSaleInfo(page, queryCondition);
    }


    @GetMapping("/getOrgType")
    @ApiOperation("所属区域")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string",
            paramType = "header")})
    public List<OrgByTypeVO> getOrgByOrgType() {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        String orgType = loginInfoDto.getOrgType().toString();
        String companyId = loginInfoDto.getCompanyId().toString();
        return orgService.getOrgByOrgType(orgType, companyId, loginInfoDto.getAppId());
    }

    /**
     * 新增-销售信息维护
     *
     * @param dealerInfoDTO
     * @author adu
     * @since 2019/10/10
     */
    @PostMapping("/addSaleInfoMaintain")
    @ResponseStatus(HttpStatus.OK)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @ApiOperation(value = "新增-销售信息维护")
    public Integer addSaleInfoMaintain(@RequestBody DealerInfoDTO dealerInfoDTO) {
        return dealerInfoService.addSaleInfoMaintain(dealerInfoDTO);
    }

    @ResponseBody
    @PutMapping(value = "/updateSaleInfo/{id}")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "编辑-销售信息维护")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    public boolean update(@PathVariable("id") Long id, @RequestBody DealerInfoDTO dealerInfoDTO) {
        dealerInfoService.updateDealerInfoFromDmss(id, dealerInfoDTO);
        return true;
    }

    /**
     * 查询-授权品牌
     *
     * @since 2019/10/10
     */
    @GetMapping(value = "/queryAuthorizedById")
    @ResponseStatus(HttpStatus.OK)
    //  @ApiOperation(value = "查询-授权品牌")
    public IPage<Map<String, Object>> queryAuthorizedById(Map<String, Object> queryCondition, @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                                                          @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        Page<Map<String, Object>> page = new Page<>(currentPage, pageSize);
        queryCondition.put("orgId", loginInfoDto.getOrgId());
        return authorizedInfoService.queryAuthorizedById(page, queryCondition);
    }


    /**
     * 新增-授权品牌（作廢）
     *
     * @since 2019/10/10
     */
//    @ResponseBody
//    @PostMapping("/addAuthorizedBrand")
//    @ResponseStatus(HttpStatus.OK)
//    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string",
//            paramType = "header")})
//    @ApiOperation(value = "新增-授权品牌")
//    public Integer addAuthorizedBrand(@RequestBody TrDealerOrgRelationPO trDealerOrgRelationPO) {
//        return authorizedInfoService.addAuthorizedBrand(trDealerOrgRelationPO);
//    }

    /**
     * 删除-授权品牌
     *
     * @since 2019/10/10
     */
    @ResponseBody
    @DeleteMapping("/deleteAuthorizedBrand/{orgRelationId}")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "删除-授权品牌")
    public Integer deleteAuthorizedBrand(@PathVariable("orgRelationId") Integer orgRelationId) {
        return authorizedInfoService.deleteAuthorizedBrand(orgRelationId);
    }


    /**
     * 经销商查看
     */
    @GetMapping("/deleteAuthorizedBrand/{dealerId}")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "经销商查看")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    public DealerInfoDTO querytDealerInfo(@PathVariable(value = "dealerId") Long dealerId) {
        return dealerInfoService.querytDealerInfo(dealerId);
    }

    /**
     * 获取区域经销商
     * 李贺
     */
    @GetMapping("/dealerByCityOrArea")
    @ApiOperation(value = "获取区域经销商")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    public List<DealerOrgVO> dealerByCityOrArea(@ApiParam(value = "省id", required = false) @RequestParam(value = "provinceId", required = false) Integer provinceId,
                                                @ApiParam(value = "市id", required = false) @RequestParam(value = "cityId", required = false) Integer cityId,
                                                @ApiParam(value = "区域orgId", required = false) @RequestParam(value = "orgId", required = false) String orgId) {
        return dealerInfoService.dealerByCityOrArea(provinceId, cityId, orgId);
    }

    /**
     * 查询品牌
     *
     * @return
     */
/*    @GetMapping("/queryBrands/{brandId}")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "品牌-查询")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    public BrandCacheVO queryBrandsInfo(@PathVariable(value = "brandId") String brandId) {
        return baseCommonBrandService.getValueByBrandId(brandId);
    }*/

    /**
     * 查询车系
     *
     * @return
     */
/*    @GetMapping("/querySeriesIds/{seriesId}")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "车系-查询")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    public SeriesCacheVO querySeriesIdsInfo(@PathVariable(value = "seriesId") String seriesId) {
        //    SeriesCacheVO 车系
        return baseCommonSeriesService.getValueBySeriesId(seriesId);

    }*/



}
